package com.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.DB;
import com.tools.Constants;

public class C2S5reply extends HttpServlet {

	@Override
	public void init() throws ServletException {
		Constants.init();
		System.out.println("DB name:" + Constants.DBNAME);
		DB.connDB();
		super.init();
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HashMap<String, String> friendsParam = new HashMap<String, String>();

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		Enumeration<String> er = request.getParameterNames();
		System.out.println("[vswb5] whitebox5 post begin");
		PrintWriter writer = response.getWriter();
		String user_id = null;
		String reply = null;
		String cmd = null;
		String friends_id = null;
		while (er.hasMoreElements()) {
			String element = er.nextElement();

			/**
			 * if his friends to look at a image,return the currect friends
			 * image path.
			 */
			if (element.equals("user_id")) {
				user_id = request.getParameterValues("user_id")[0];
			}
			if (element.equals("reply")) {
				reply = request.getParameterValues("reply")[0];
			}
			if (element.equals("cmd")) {
				cmd = request.getParameterValues("cmd")[0];
			}
			if (element.equals("friends_id")) {
				friends_id = request.getParameterValues("friends_id")[0];
			}

		}

		if ((user_id != null) && (reply != null) && cmd.equals("1") && (friends_id != null)) {
			ResultSet rs = DB.select("select time from friends where user_id='" + user_id + "' and _id='" + friends_id + "';");
			try {
				if (rs != null && rs.next()) {
					DB.insert("insert into friends(_id,user_id,reply,time) values('" + friends_id + "','" + user_id + "','" + reply + "',now());");
				} else {
					DB.update("update friends set reply='" + reply + "' where user_id='" + user_id + "' and _id='" + friends_id + "';");
					DB.update("update friends set time=now() where user_id='" + user_id + "' and _id='" + friends_id + "';");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		Enumeration<String> er = request.getParameterNames();
		System.out.println("[vswb5] whitebox5 post begin");
		PrintWriter writer = response.getWriter();
		String user_id = null;
		String reply = null;
		String replys_id = null;
		String cmd = null;
		String time = null;
		while (er.hasMoreElements()) {
			String element = er.nextElement();

			/**
			 * if his friends to look at a image,return the currect friends
			 * image path.
			 */
			if (element.equals("user_id")) {
				user_id = request.getParameterValues("user_id")[0];
			}
			if (element.equals("cmd")) {
				cmd = request.getParameterValues("cmd")[0];
			}
			if (element.equals("time")) {
				time = "time";
			}
		}
		if ((user_id != null) && (cmd != null) && cmd.equals("2")) {
			ResultSet rs = DB.select("select reply,user_id from friends where _id='" + user_id + "';");
			try {
				while (rs.next()) {
					reply = rs.getString("reply");
					replys_id = rs.getString("user_id");
					if (!reply.equals("") && reply != null) {
						writer.println(replys_id + ":>" + reply + ",");
						System.out.println(reply);
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		if (time != null) {
			ResultSet rs1 = DB.select("select time from friends where _id='" + user_id + "' and reply<>'';");
			try {
				while (rs1.next()) {
					time = rs1.getString("time");
					if (!time.equals("") && time != null) {
						writer.println(time + ",");
						System.out.println(time);
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					rs1.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	
}
